package cn.wp.common.core.plugin.fileserver.strategy;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.util.StrUtil;
import cn.wp.common.core.exception.WpException;
import cn.wp.common.core.plugin.fileserver.FileInfo;
import cn.wp.common.core.plugin.fileserver.StorageProcessHandler;
import cn.wp.common.core.plugin.fileserver.StorageStrategy;
import cn.wp.common.core.plugin.fileserver.StrategyParameter;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期存储策略
 * 按日期分目录存储
 * Created by chenyb on 2019/09/30
 */
public class DateStrategy extends StorageStrategy {

    public void store(File file,StrategyParameter parameter,FileInfo fileInfo, String rootPath) {
        // 原字符串格式 date:yyyy-MM
        String dateFormatStr = fileServerProperties.getStorageStrategy().split(":")[1];
        if(StrUtil.isBlank(dateFormatStr)){
            throw new WpException("fileserver.storage-strategy.format value error, for example: date:yyyy-MM");
        }
        String folder = DateUtil.format(new Date(), new SimpleDateFormat(dateFormatStr));
        String contextPath = "/" + folder;
        String parentPath = rootPath + contextPath;
        // 创建目录
        mkdir(parentPath);
        String fullPath = parentPath + "/" + fileInfo.getName();
        // 设置文件路径
        fileInfo.setPath(contextPath);
        fileInfo.setFullPath(fullPath);
        fileInfo.setParentDirectory(parentPath);
        // 文件拷贝
        FileUtil.copy(file.getAbsolutePath(),fullPath,true);
    }

    @Override
    public void store(InputStream inputStream, StrategyParameter parameter, FileInfo fileInfo, StorageProcessHandler storageProcessHandler) {
        // 原字符串格式 date:yyyy-MM
        String dateFormatStr = fileServerProperties.getStorageStrategy().split(":")[1];
        if(StrUtil.isBlank(dateFormatStr)){
            throw new WpException("fileserver.storage-strategy.format value error, for example: date:yyyy-MM");
        }
        String folder = DateUtil.format(new Date(), new SimpleDateFormat(dateFormatStr));
        String contextPath = "/" + folder;
        String parentPath = parameter.getRootPath() + contextPath;
        // 创建目录
        mkdir(parentPath);
        String fullPath = parentPath + "/" + fileInfo.getName();
        // 设置文件路径
        fileInfo.setPath(contextPath + "/" + fileInfo.getName());
        fileInfo.setFullPath(fullPath);
        fileInfo.setParentDirectory(parentPath);
        // 文件拷贝
        try {
            FileUtil.newFile(fullPath);
            OutputStream out = new FileOutputStream(fullPath);
            IoUtil.copy(inputStream, out,IoUtil.DEFAULT_BUFFER_SIZE,new StreamProgress() {
                @Override
                public void start() {
                    if(storageProcessHandler != null){
                        storageProcessHandler.start(fileInfo);
                    }
                }

                @Override
                public void progress(long l) {
                    if(storageProcessHandler != null){
                        fileInfo.setUploadSize(l);
                        storageProcessHandler.process(fileInfo);
                    }
                }

                @Override
                public void finish() {
                    if(storageProcessHandler != null){
                        storageProcessHandler.finish(fileInfo);
                    }
                }
            });
            IoUtil.close(inputStream);
            IoUtil.close(out);
        } catch (FileNotFoundException e) {
           throw new WpException("上传目标文件未找到",e);
        }
    }

    public static void main(String[] args) {
        System.err.println(FileUtil.mkdir("F:/ddd/test/eee"));
    }

}
